$(function(){
	var basePath=$("#basepath").val();
	$("#admin_add").click(function(){
		$("#admin_add_box input[name='no']").val("");
		$("#admin_add_box input[name='name']").val("");
		$("#admin_add_box input[name='password']").val("");
		$("#admin_add_box input[name='gender']").prop("checked",false);
		$("#admin_add_box input[name='gender'][value='男']").prop("checked",true);
		$("#admin_add_box input[name='telephone']").val("");
		$("#admin_add_box select[name='college'] option").prop("selected",false);
		$("#admin_add_box select[name='college'] option:first").prop("selected",true);
	});
	$("#admin_add_box_save").click(function(){
		var adminNo=$("#admin_add_box input[name='no']").val();
		var adminName=$("#admin_add_box input[name='name']").val();
		var adminPass=$("#admin_add_box input[name='password']").val();
		var adminGender=$("#admin_add_box input[name='gender']:checked").val();
		var adminTel=$("#admin_add_box input[name='telephone']").val();
		var adminClgId=$("#admin_add_box select[name='college']").val();
		$.post(basePath+ "admin/admin/insertAdmin",
				{'no':adminNo,'name':adminName,'password':adminPass,'gender':adminGender,'telephone':adminTel,'collegeId':adminClgId},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (parseInt(data)) {
						case -1:
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case -2:
							alert("除电话号码外,其余均为必填项,请检查");
							break;
						case -3:
							alert("已存在账号为:"+adminNo+" 的管理员,请重新输入");
							break;
						case 0:
							noty({text:"添加成功,正在刷新"});
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
				},"text");
	});
	$(".admin_delete").click(function(){
		var adminId=$(this).attr("data-adminId");
		$("#admin_delete_box input[name='adminId'").val(adminId);
	});
	$("#admin_delete_box_delete").click(function(){
		var adminId=$("#admin_delete_box input[name='adminId'").val();
		$.get(basePath+ "admin/admin/deleteAdmin/"+adminId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (parseInt(data)) {
						case -1:
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case -2:
							alert("您不能删除自己");
							history.go(0);
							break;
						case 0:
							noty({text:"删除成功,正在刷新"});
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	$(".admin_edit").click(function(){
		var adminId=$(this).attr("data-adminId");
		$("#admin_edit_box input[name='adminId']").val(adminId);
		$.get(basePath+ "admin/admin/getAdminByAdminId/"+adminId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0||data==null||data==""){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						$("#admin_edit_box input[name='no']").val(data.no);
						$("#admin_edit_box input[name='name']").val(data.name);
						$("#admin_edit_box input[name='gender'][value='"+data.gender+"']").prop("checked",true);
						$("#admin_edit_box input[name='telephone']").val(data.telephone);
						if(data.superAdmin){
							$("#admin_edit_box select[name='college']").val(0);
						}else{
							$("#admin_edit_box select[name='college']").val(data.college.id);
						}
					}
			}, "json");
	});
	$("#admin_edit_box_save").click(function(){
		var adminId=$("#admin_edit_box input[name='adminId']").val();
		var adminNo=$("#admin_edit_box input[name='no']").val();
		var adminName=$("#admin_edit_box input[name='name']").val();
		var adminGender=$("#admin_edit_box input[name='gender']:checked").val();
		var adminTel=$("#admin_edit_box input[name='telephone']").val();
		var adminClgId=$("#admin_edit_box select[name='college']").val();
		$.post(basePath+ "admin/admin/updateAdmin",
				{'no':adminNo,'name':adminName,'id':adminId,'gender':adminGender,'telephone':adminTel,'collegeId':adminClgId},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (parseInt(data)) {
						case -1:
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case -2:
							alert("除电话号码外,其余均为必填项,请检查");
							break;
						case -3:
							alert("已存在账号为:"+adminNo+" 的管理员,请重新输入");
							break;
						case 0:
							noty({text:"添加成功,正在刷新"});
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
				},"text");
	});
	$(".admin_reset").click(function(){
		var adminId=$(this).attr("data-adminId");
		$("#admin_reset_box input[name='adminId'").val(adminId);
		var pass=$("#admin_reset_box input[name='password'").val("");
	});
	$("#admin_reset_box_save").click(function(){
		var adminId=$("#admin_reset_box input[name='adminId'").val();
		var pass=$("#admin_reset_box input[name='password'").val();
		$.get(basePath+ "admin/admin/resetPass/",{"id":adminId,"pass":pass},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (parseInt(data)) {
						case -1:
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case 0:
							noty({text:"修改成功,正在刷新"});
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	$("#sysset_back").click(function(){
		$("#sysset_confirm_box input[name='type']").val(1);
		$("#sysset_confirm_box input[name='password']").val("");
		$("#sysset_confirm_box .modal-header h3").html("确认执行归档操作");
	});
	$("#sysset_delete_back").click(function(){
		$("#sysset_confirm_box input[name='type']").val(2);
		$("#sysset_confirm_box input[name='password']").val("");
		$("#sysset_confirm_box .modal-header h3").html("确认要清空归档");
	});	
	$("#sysset_delete_teacher").click(function(){
		$("#sysset_confirm_box2 input[name='type']").val(3);
		$("#sysset_confirm_box2 input[name='password']").val("");
		$("#sysset_confirm_box2 select[name='college']").val(0);
		$("#sysset_confirm_box2 .modal-header h3").html("确认要清空教师");
	});
	$("#sysset_confirm_box_confirm").click(function(){
		var type=$("#sysset_confirm_box input[name='type']").val();
		var password=$("#sysset_confirm_box input[name='password']").val();
		$.get(basePath+ "admin/admin/clearData/",{"type":type,"password":password},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (parseInt(data)) {
						case -1:
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case -2:
							alert("密码错误!");
							$("#sysset_confirm_box").modal("hide");
							break;
						case 0:
							noty({text:"操作成功"});
							$("#sysset_confirm_box").modal("hide");
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});	$("#sysset_confirm_box2_confirm").click(function(){
		var type=$("#sysset_confirm_box2 input[name='type']").val();
		var password=$("#sysset_confirm_box2 input[name='password']").val();
		var collegeId=$("#sysset_confirm_box2 select[name='college']").val();
		$.get(basePath+ "admin/admin/clearData/",{"type":type,"password":password,"collegeId":collegeId},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (parseInt(data)) {
						case -1:
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case -2:
							alert("密码错误!");
							$("#sysset_confirm_box2").modal("hide");
							break;
						case 0:
							noty({text:"操作成功"});
							$("#sysset_confirm_box2").modal("hide");
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
});